//
//  SINAActivityTargetTableViewCell.m
//  PlanTech
//
//  Created by  androidlongs on 2018/6/28.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINAActivityTargetTableViewCell.h"
#import "SINACircularBeadUtils.h"
#import "SINACalendarUtils.h"

@interface SINAActivityTargetTableViewCell()
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIView *addView;

@property (weak, nonatomic) IBOutlet UILabel *targetTitleLabel;
//实际用时
@property (weak, nonatomic) IBOutlet UILabel *useTimeLabe;
//目标时间
@property (weak, nonatomic) IBOutlet UILabel *tarTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *tarTimeTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *statueLabel;
@property (weak, nonatomic) IBOutlet UIImageView *statueImageView;


@end

@implementation SINAActivityTargetTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [SINACircularBeadUtils makeCircularBead:2 andView: self.mainView andIsSetBackground:NO];
    [SINACircularBeadUtils makeCircularBead:2 andView: self.addView andIsSetBackground:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

-(void)setModel:(SINAStuActivityTargetModel *)model{
    _model = model;
    
    if (model.targetPageType==0) {
        self.addView.hidden=YES;
        self.mainView.hidden=NO;
        [[SINAUiviewClickLiserner alloc]viewClickFunction:self.addView andWithBlick:^(UIView *view) {
        }];
        
        [[SINAUiviewClickLiserner alloc]viewClickFunction:self.mainView andWithBlick:^(UIView *view) {
            if (self.cellTargetClickBlock) {
                self.cellTargetClickBlock(1, model);
            }
        }];
        
        self.targetTitleLabel.text=model.title;
        if (model.status==1000) {
            //未开始
            self.useTimeLabe.text=@"00'00'00";
            self.statueLabel.text=@"未开始";
            self.statueLabel.textColor=[UIColor colorWithHexString:@"#747474"];
            self.statueImageView.image=[UIImage imageNamed:@"tag_no_start_icon"];
            if (model.plantime!=nil&&![model.plantime isEqualToString:@""]) {
                self.tarTimeTitleLabel.hidden=NO;
                self.tarTimeLabel.hidden=NO;
                self.tarTimeLabel.text=model.plantime;
            } else {
                self.tarTimeTitleLabel.hidden=YES;
                self.tarTimeLabel.hidden=YES;
            }
        }else if (model.status==1012||model.status==1011){
            //进行中
            self.statueLabel.text=@"进行中";
            self.statueLabel.textColor=[UIColor colorWithHexString:@"#26bad1"];
            self.statueImageView.image=[UIImage imageNamed:@"tag_start_icon"];
        }else if (model.status==1013){
            //暂停
            self.statueLabel.text=@"进行中";
            self.statueLabel.textColor=[UIColor colorWithHexString:@"#26bad1"];
            self.statueImageView.image=[UIImage imageNamed:@"tag_start_icon"];
        }else if (model.status==1014){
            //完成
            self.statueLabel.textColor=[UIColor colorWithHexString:@"#2b89f5"];
            self.statueImageView.image=[UIImage imageNamed:@"tag_finish_icon"];
            if (model.plantime==nil||[model.plantime isEqualToString:@""]) {
                self.statueLabel.text=@"已成功";
            } else {
                self.statueLabel.text=@"已达标";
            }
        }
        
        
        if (model.plantime!=nil&&![model.plantime isEqualToString:@""]) {
            self.tarTimeTitleLabel.hidden=NO;
            self.tarTimeLabel.hidden=NO;
            self.tarTimeLabel.text=model.plantime;
        } else {
            self.tarTimeTitleLabel.hidden=YES;
            self.tarTimeLabel.hidden=YES;
        }
        
        NSInteger useTime = self.model.useTime;
        
        if (self.model.status==1000) {
            //未开始  显示开始按钮
            self.useTimeLabe.text=@"00:00:00";
        }else if (self.model.status==1012||self.model.status==1011) {
            //已开始  显示暂按钮
            
            if (useTime==0) {
                //未暂停过
                useTime =[[SINACalendarUtils currentTimeStr]integerValue]-self.model.startTime;
                self.useTimeLabe.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",useTime/1000]];
            }else{
                //有过暂停时间
                useTime = useTime+[[SINACalendarUtils currentTimeStr]integerValue]-self.model.pauseendtime;
                self.useTimeLabe.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",useTime/1000]];
            }
            
        }else if (self.model.status==1013 ) {
            //已暂住  显示继续按钮
            //一定有暂停时间
            self.useTimeLabe.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",useTime/1000]];
        }else if (self.model.status==1014) {
            //已暂住  显示继续按钮
            //一定有暂停时间
            self.useTimeLabe.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",useTime/1000]];
        }
        
        
        NSInteger activityStartTime = [self.activityModel.activityStartTime integerValue];
        NSInteger activityEndTime = [self.activityModel.activityStopTime integerValue];
        //当前时间
        NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
        
        if (currentTime<activityStartTime) {
            DLog(@"活动未开始");
        }else if(currentTime>=activityStartTime&&currentTime<=(activityEndTime)){
            DLog(@"活动中");
        }else if(currentTime>=activityEndTime&&currentTime<=(activityEndTime+30*60*1000)){
            //活动结束 以及结束后的半小时
        }else{
            activityEndTime=activityEndTime+30*60*1000;
            //超时
            if (self.model.status!=1014) {
                self.statueLabel.textColor=[UIColor redColor];
                self.statueImageView.image=[UIImage imageNamed:@"tag_no_finish_icon"];
                if (model.plantime==nil||[model.plantime isEqualToString:@""]) {
                    self.statueLabel.text=@"未成功";
                } else {
                    self.statueLabel.text=@"未达标";
                }
            }
            
            useTime = self.model.useTime;
            
            //超时下设置实际用时
            if (self.model.status==1000) {
                //未开始  显示开始按钮
                self.useTimeLabe.text=@"00:00:00";
            }else if (self.model.status==1012||self.model.status==1011) {
                //已开始  显示暂按钮
                
                if (useTime==0) {
                    //未暂停过
                    useTime =activityEndTime-self.model.startTime;
                    if(useTime<0){
                        useTime=0;
                    }
                    self.useTimeLabe.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",useTime/1000]];
                }else{
                    //有过暂停时间
                    useTime = useTime+activityEndTime-self.model.pauseendtime;
                    self.useTimeLabe.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",useTime/1000]];
                }
                
            }else if (self.model.status==1013 ) {
                //已暂住  显示继续按钮
                //一定有暂停时间
                self.useTimeLabe.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",useTime/1000]];
            }else if (self.model.status==1014) {
                //已暂住  显示继续按钮
                //一定有暂停时间
                self.useTimeLabe.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",useTime/1000]];
            }
            
            
            
        }
        
    } else {
        self.addView.hidden=NO;
        self.mainView.hidden=YES;
        
        [[SINAUiviewClickLiserner alloc]viewClickFunction:self.addView andWithBlick:^(UIView *view) {
            if (self.cellTargetClickBlock) {
                self.cellTargetClickBlock(0, model);
            }
        }];
    }
    
    
    
    
}
//3 重写setFrame方法
-(void)setFrame:(CGRect)frame
{
    //只修改高度
    frame.size.height-=5;
    //调用系统方法设置
    [super setFrame:frame];
}
@end
